﻿using System;
using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto;
using Best.SmartCms.Dto.Common;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service
{
    /// <summary>
    /// Channel服务
    /// </summary>
    public class ChannelService : IService
    {
        private readonly ChannelDao _channelDao;
        private readonly ChannelGroupDao _channelGroupDao;
        private readonly WorkContext _workContext;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        /// <param name="workContext"></param>
        public ChannelService(MySqlDbContext mySqlDbContext, WorkContext workContext)
        {
            _workContext = workContext;
            _channelDao = new ChannelDao(mySqlDbContext);
            _channelGroupDao = new ChannelGroupDao(mySqlDbContext);

        }

        

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<ChannelListDto> PageList(ChannelListQueryDto query)
        {
            var list = _channelDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<Channel>, List<ChannelListDto>>(list);
            foreach (var item in pageList)
            {
                item.ChannelGroupName = _channelGroupDao.GetSingle(item.ChannelGroupId)?.Name;
            }

            return new PagedResponse<ChannelListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(ChannelEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<ChannelEditDto, Channel>(dto);
            entity.CreatedTime = DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _channelDao.Add(entity);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(ChannelEditDto dto)
        {
            var entity = _channelDao.GetSingle(dto.Id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{dto.Id}的数据");
            }

            TinyMapperExtensions.Map(dto, entity);
            
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _channelDao.Update(entity);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ChannelEditDto Info(long id)
        {
            var entity = _channelDao.GetSingle(id);
            return TinyMapperExtensions.Map<Channel, ChannelEditDto>(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _channelDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _channelDao.Delete(entity);
        }

        /// <summary>
        /// 选择项
        /// </summary>
        /// <returns></returns>
        public List<SelectOptionDto> Option()
        {
            var list = _channelDao.FindAll();
            return list.OrderBy(p=>p.OrderNo).Select(p => new SelectOptionDto {Label = p.Name, Value = p.Id.ToString()}).ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ChannelListDto> FindAll()
        {
            var list = _channelDao.FindAll();
            return TinyMapperExtensions.Map<List<Channel>, List<ChannelListDto>>(list);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public List<ChannelListDto> ListByGroupId(long groupId)
        {
            var list = _channelDao.FindByGroupId(groupId);
            return TinyMapperExtensions.Map<List<Channel>, List<ChannelListDto>>(list);
        }
    }
}
